package com.forlove.app.common.base.mvp.service.impl;

import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.forlove.app.common.base.mvp.service.IBaseService;
import com.forlove.app.common.image.ImageLoader;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.network.OkhttpManager;
import com.forlove.app.common.utils.StringUtil;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;

import java.lang.ref.WeakReference;
import java.util.Map;

/**
 * Created by yx on 2017/6/9.
 */

public abstract class BaseServiceImpl<T> implements IBaseService<T> {

    @Override
    public synchronized void loadImageView(Context context, View view, String Url, int defaultImage) {
        // LogUtil.e("yjl", "loadImageView");
        if (!TextUtils.isEmpty(Url) && !StringUtils.isUrlValid(Url)
                && !StringUtil.isFilePathExist(Url)) {
            //TODO 如果不是完整的url,则拼接ur，这个版本都是拼接URL
            Url = ServerManager.getImageUrl() + Url;
        }
        WeakReference<View> imageViewWeakReference = new WeakReference<>(view);
        WeakReference<Context> contextWeakReference = new WeakReference<>(context);
        View weakView = imageViewWeakReference.get();
        if (weakView != null) {
            if (defaultImage > 0) {
                if (weakView instanceof AppCompatImageView) {
                    ((AppCompatImageView) weakView).setImageResource(defaultImage);
                } else {
                    weakView.setBackgroundResource(defaultImage);
                }
            }
            if (StringUtil.isUrl(Url)
                    && view instanceof ImageView
                    && contextWeakReference.get() != null) {
                try {
                    OkhttpManager.getInstance().loadImage(contextWeakReference.get(), Url, (ImageView) weakView, defaultImage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (StringUtil.isUrl(Url)) {
                ImageLoader.getInstance().loadImage(Url, weakView, true);
            } else if (StringUtil.isFilePath(Url)) {
                ImageLoader.getInstance().loadImage(Url, weakView, false);
            }
        }

    }

    @Override
    public synchronized void loadData(String action, Map param, ICallBack mCallBack) {
        try {
            NetworkManager.getInstance().loadDataByPost(action, param, mCallBack);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public synchronized void uploadFile(String action, Map param, ICallBack iCallBack) {
        try {
            NetworkManager.getInstance().postPic(action, param, iCallBack);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clearImageCache(String path) {
        try {
            NetworkManager.getInstance().clearImageCache(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
